package cn.house.houseai.service.impl;

import cn.house.houseai.entify.Device;
import cn.house.houseai.mapper.DeviceMapper;
import cn.house.houseai.service.DeviceService;
import cn.house.houseai.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 羡y
 * @date 2024/9/30
 * @Description
 */
@Service
public class DeviceServiceimpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public ResultVo queryId(Integer userid){
        List<Device> list = null;
        try {
            list = deviceMapper.queryId(userid);
            return new ResultVo(100, list, "查询成功");
        } catch (Exception e) {
            return new ResultVo(101, e.getMessage(), "查询失败: "  );
        }
    }

    @Override
    public ResultVo insertDevice(int user_id, Device device) {
        ResultVo vo;

        if (device == null) {
            return new ResultVo(101, null, "设备信息不能为空");
        }

        device.setUser_id(user_id); // 设置 user_id 到设备对象

        try {
            deviceMapper.insertDevice(user_id, device); // 调用 mapper 插入设备
            vo = new ResultVo(100, device, "添加设备成功");
        } catch (Exception e) {
            vo = new ResultVo(101, e.getMessage(), "添加设备失败");
        }

        return vo;
    }

    @Override
    public ResultVo updateDevice(int user_id, Device device) {
        device.setUser_id(user_id);  // 确保 ID 被设置
        int result = deviceMapper.updateDevice(user_id, device);
        if (result > 0) {
            return new ResultVo(100, device, "设备信息更新成功");
        } else {
            return new ResultVo(101, null, "设备信息更新失败");
        }
    }

}
